package com.wkw.jkyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkw.jkyf.mapper.DruginfoMapper;
import com.wkw.jkyf.mapper.ProblemMapper;
import com.wkw.jkyf.pojo.Druginfo;
import com.wkw.jkyf.pojo.Problem;
import com.wkw.jkyf.pojo.Saleinfo;
import com.wkw.jkyf.service.IProblemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ProblemServiceImpl extends ServiceImpl<ProblemMapper, Problem> implements IProblemService {
    @Resource
    private ProblemMapper problemMapper;
    @Resource
    private DruginfoMapper druginfoMapper;
    @Override
    public IPage<Problem> selectProblemPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Problem> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.like("dname",param);
        }
        queryWrapper.orderByDesc("id");
        Page<Problem> problemPage = new Page<>();

        return problemMapper.selectPage(problemPage,queryWrapper);
    }

    @Override
    @Transactional
    //添加问题药品记录
    public int addProblem(Problem problem) {
        int operateFlag = 0;
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",problem.getDname())
                .eq("number",problem.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        //药品库存数量
        int stock = druginfo.getStock();
        //问题药品数量
        int saleCount = problem.getDcount();
        if(saleCount>stock){
            return -1;
        }
        druginfo.setStock(stock-saleCount);
        operateFlag += druginfoMapper.updateById(druginfo);
        Float price = druginfo.getPrice();
        float resultTotal = saleCount * price;
        problem.setTotal(Double.valueOf(resultTotal+""));
        problem.setDprice(price);
        problem.setCreatetime(new Date());
        operateFlag += problemMapper.insert(problem);
        return operateFlag;
    }

    @Override
    @Transactional
    public int editProblem(Problem problem) {
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",problem.getDname())
                .eq("number",problem.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);

        QueryWrapper<Problem> problemWrapper = new QueryWrapper<>();
        problemWrapper
                .eq("id",problem.getId());
        //当前数据库的问题记录信息
        Problem oldProblem = problemMapper.selectOne(problemWrapper);
        if(!oldProblem.getDnumber().equals(problem.getDnumber())){
            return -2;
        }
        //当前数据库的问题药品数量
        int oldCount = oldProblem.getDcount();
        //新的问题药品数量
        int newCount = problem.getDcount();
        if(newCount<oldCount){
            druginfo.setStock(druginfo.getStock()+(oldCount-newCount));
            druginfoMapper.updateById(druginfo);
        }else if(newCount>oldCount){
            int newSaleCount = newCount - oldCount;
            if(newSaleCount>druginfo.getStock()){
                return -1;
            }
            druginfo.setStock(druginfo.getStock()-newSaleCount);
            druginfoMapper.updateById(druginfo);
        }

        //总金额
        float finalTotal = problem.getDcount() * druginfo.getPrice();
        problem.setTotal(Double.valueOf(finalTotal+""));
        problem.setCreatetime(new Date());
        return problemMapper.updateById(problem);
    }

    @Override
    public Problem queryProblemById(Integer id) {
        return problemMapper.selectById(id);
    }

    @Override
    @Transactional
    public int deleteProblemById(Integer id) {
        Problem problem = problemMapper.selectById(id);
        //查询出来的数量
        Integer count = problem.getDcount();
        QueryWrapper<Druginfo> drugWrapper = new QueryWrapper<>();
        drugWrapper
                .eq("name",problem.getDname())
                .eq("number",problem.getDnumber());
        Druginfo druginfo = druginfoMapper.selectOne(drugWrapper);
        druginfo.setStock(druginfo.getStock()+count);
        druginfoMapper.updateById(druginfo);
        return problemMapper.deleteById(id);
    }
}
